package view.plugin
{
	//右上角拉力赛信息
	
	import fl.controls.TextArea;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.Timer;
	
	import flash3.framework.Map;
	import flash3.framework.Singleton;
	import flash3.net.*;
	
	import global.Resource;
	
	import model.GameModel;
	
	import net.*;
	
	import util.RandomUtil;
	
	import vo.ListPoolItem;
	import vo.Message;
	import vo.MoneyPoolItem;
	import vo.UserRankInfo;

	public class RallyMessagePanle extends Sprite
	{
		private var panel:MovieClip = null;
		private var textArea:TextField = null;
		private var timer:Timer = null;
		private var outTimer:Timer = null;
		private var TICK_TIME:uint = 50000;
		private var OUT_TICK_TIME:uint = 15000;
//		private var TICK_TIME:uint = 8000;
//		private var OUT_TICK_TIME:uint = 2000;
		private var sendList:Array = [];
		private var outList:Map = new Map();
		public function RallyMessagePanle()
		{
			super();
			panel = Resource.i.getMovieClip("common", "ResPanel");
			textArea = new TextField();
			textArea.width = panel.width - 10;
			textArea.height = panel.height - 5;
			textArea.x = 5;
			textArea.y = 1;
			textArea.wordWrap=true;
			textArea.multiline = true;
			var tf:TextFormat = new TextFormat("verdana",14, 0xF7DF27);
//			tf.align = "center";
			tf.leading = 6.5;
			textArea.defaultTextFormat = tf;
			addChild(panel);
			addChild(textArea);
			this.addEventListener(Event.ADDED_TO_STAGE, onAddToStage);
			this.addEventListener(Event.REMOVED_FROM_STAGE, onRemoveFromStage);
			sendList.push({mainid:UserGroup.MDM_GR_USER, subid:UserGroup.SUB_GR_USER_RANKINFO_REQ});
			sendList.push({mainid:UserGroup.MDM_GR_USER, subid:UserGroup.SUB_GR_USER_MONEYPOOL_REQ});
			timer = new Timer(TICK_TIME);
			outTimer = new Timer(OUT_TICK_TIME);
			
			this.x = 515; this.y = 80;
		}
		private function onAddToStage(evt:Event):void
		{
			clear();
			
			NetFacade.i.acall(UserGroup.MDM_GR_USER,UserGroup.SUB_GR_USER_RANKINFO,{call:onUserRankInfoRequest,forever:true});
			NetFacade.i.acall(UserGroup.MDM_GR_USER,UserGroup.SUB_GR_USER_MONEYPOOL,{call:onUserPrizePoolReply,forever:true});
			NetFacade.i.acall(SystemGroup.MDM_GR_SYSTEM,SystemGroup.SUB_GR_MESSAGE,{call:onNotifySystemMessage,forever:true});
			getInfo();
			
			timer.addEventListener(TimerEvent.TIMER, onTimerComplete);
			timer.reset();
			timer.start();
			outTimer.addEventListener(TimerEvent.TIMER, onOutTimerComplete);
			outTimer.reset();
			outTimer.start();
			
//			var message:Message = new Message();
//			message.szContent = "ddddddddddd";
//			message.wMessageType = Message.SMT_REWARDINFO;
//			systemMessage(message);
		}
		private function onRemoveFromStage(evt:Event):void
		{
			NetFacade.i.rcall(UserGroup.MDM_GR_USER,UserGroup.SUB_GR_USER_RANKINFO);
			NetFacade.i.rcall(UserGroup.MDM_GR_USER,UserGroup.SUB_GR_USER_MONEYPOOL);
			NetFacade.i.rcall(SystemGroup.MDM_GR_SYSTEM,SystemGroup.SUB_GR_MESSAGE);
			timer.removeEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
			timer.stop();
			outTimer.removeEventListener(TimerEvent.TIMER, onOutTimerComplete);
			outTimer.stop();
		}
		private function onNotifySystemMessage(...parms):void
		{
			var message:Message = parms[0]._parm as Message;
			if(!message) return;
			systemMessage(message);
		}
		private function systemMessage(message:Message):void
		{
			if((message.wMessageType & Message.SMT_REWARDINFO) != 0)
			{
				appendText("[奖励]:"+message.szContent);
			}
			else
			{
				appendText("[系统]:"+message.szContent);
			}
		}
		public function appendAward(award:uint, score:int):String
		{
			var prizePool:MoneyPoolItem = GameModel.i.poolItem;
			return prizePool.getTips(award, score);
		}
		
		public function appendRank(cur:uint, max:uint):String
		{
			if(cur == 0) return null;
			var str:String = "您当前的排名是 "+cur+"，位于 "+cur+"/"+max;
			return str;
		}
		
		public function appendRankScore(score:int, cur:uint, max:uint):String
		{
			if(cur == 0) return null;
			var str:String = "您当前的排名分是 "+score+"，位于 "+cur+"/"+max;
			return str;
		}
		
		public function appendText(str:String):void
		{
			textArea.appendText(str + "\n\n");
			textArea.scrollV = textArea.maxScrollV;
//			var htmlText:String = textArea.htmlText;
//			htmlText += str;
//			textArea.htmlText = htmlText;
//			textArea.scrollV = textArea.maxScrollV;
		}
		
		public function clear():void
		{
			outList.clear();
			textArea.text = "";
		}
		
		public function onUserPrizePoolReply(packet:MsgPacket):void
		{
			var prizePool:MoneyPoolItem = packet._parm as MoneyPoolItem;
			var item:Array = prizePool.listPoolItem;
			for(var i:uint = 0; i < item.length; i++)
			{
				var str:String = appendAward(i, item[i].money);
				if(str) outList.put(i, str);
			}
		}
		public function onUserRankInfoRequest(packet:MsgPacket):void
		{
			var rankInfo:UserRankInfo = packet._parm as UserRankInfo;
			var str:String = appendRank(rankInfo.rank, rankInfo.rankCount);
			if(str && str.length != 0) outList.put(100, str);
			str = appendRankScore(rankInfo.score, rankInfo.rank, rankInfo.rankCount);
			if(str && str.length != 0) outList.put(101, str);
		}
		
		private function onTimerComplete(evt:TimerEvent):void
		{
			getInfo();
		}
		
		private function onOutTimerComplete(evt:TimerEvent):void
		{
			var arr:Array = outList.keys();
			for(var i:uint = 0; i < arr.length; i++)
			{
				var str:String  = outList.remove(arr[i])  as String;
				if(str && str.length != 0)
				{
					appendText(str);
					break;
				}
			}
		}
		
		private function getInfo():void
		{
			for(var i:uint = 0; i < sendList.length; i++)
			{
				NetFacade.i.send(sendList[i].mainid, sendList[i].subid);
			}
		}
		
		public function show(visible:Boolean):void
		{
			if(visible)
				ChessLobby.chat.addChild(this);
			else if(parent)
				parent.removeChild(this);
		}

		static public function get i():RallyMessagePanle
		{
			return Singleton.getInstanceOrCreate(RallyMessagePanle) as RallyMessagePanle;
		}
	}
}